
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  defaultproperty;

import java.text.SimpleDateFormat;
import java.util.Hashtable;
import java.util.Vector;

import logger.PoolLogger;

import pool.tablename.Pool;
import query.view.Query;
import utility.CapitalChar;
import utility.ConvertToLower;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.GetIdName;
import defaultproperty.classes.Class;

public class GetDefaultvalue  
{
	String property ="property";
	String value = "value";
	String CLASS="class";
	String REPORT="report";      // This line is added in code , 1st change.
	Pool masterobject;
	GetIdName gid;
	String userid="userid";
	String TYPE = "type";
	PoolLogger pl;
	
	public GetDefaultvalue(Object object) 
	{
		masterobject  = (Pool) object;
		
		gid =new GetIdName();
		userid = gid.getId(userid);
		
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	public String getdefaultvalue(String typeofvalue,String value)
	{
		typeofvalue = gid.getItem(typeofvalue);
		if(typeofvalue.equalsIgnoreCase(this.value))
			return gid.getItem(value);
		else if(typeofvalue.equalsIgnoreCase(property))
		{
			String user =  masterobject.getRequestParameter().getUsername();
			user = gid.getId(user);
			RecordSearch rs =new RecordSearch();
			Hashtable<Object, Object> table =new Hashtable<Object, Object>();
			table.put("mid", user);
			table.put("pid", value);
			table.put("td", "null");
			rs.setConditionMap(table);
			String line[] = rs.getArrayFromResultSet();
			if(line.length==0)
			{
				table.put("pid", userid);
				rs.setConditionMap(table);
				String result[] = rs.getArrayFromResultSet();
				for(int i=0;i<line.length;i++)
				{
					String splitline[] = result[i].split("\t");
					table.clear();
					table.put("mid", splitline[Column.pv_index-1]);
					table.put("pid", value);
					rs.setConditionMap(table);
					line = rs.getArrayFromResultSet();
				}
			}
			for(int i=0;i<line.length;i++)
			{
				String splitline[] = line[i].split("\t");
				String pv =splitline[Column.pv_index-1];
				if(pv.equals("0"))
					pv = splitline[Column.vt_index-1];
				else
					pv= gid.getItem(pv);
				return "pv";
			}
			return "error";
		}
		else if(typeofvalue.equalsIgnoreCase(CLASS))
		{
			RecordSearch rs=new RecordSearch();
			Hashtable<Object, Object> table =new Hashtable<Object, Object>();
			table.put("mid", value);
			table.put("td", "null");
			rs.setConditionMap(table);
			String line[] = rs.getArrayFromResultSet();
			table.clear();
			for(int i=0;i<line.length;i++)
			{
				String splitline[] = line[i].split("\t");
				String pv = splitline[Column.pv_index-1];
				if(pv.equals("0"))
					pv = splitline[Column.vt_index-1];
				else
					pv = gid.getItem(pv);
				table.put(gid.getItem(splitline[Column.pid_index-1]), pv);
			}
			table = ConvertToLower.convertHashKey(table);
			String classname = table.get(TYPE).toString();
			try
			{
				defaultproperty.classes.Class classobject;
				java.lang.Class c= java.lang.Class.forName("defaultproperty.classes."+CapitalChar.makeFirstCharCapital(classname));
				classobject= (Class) c.newInstance();
		 		classobject.setObject();
				classobject.setPropertymap(table,value);
				return classobject.getResult();
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		/* Here , following else if loop is also a change in this file , 2nd change. */
		
		else if(typeofvalue.equalsIgnoreCase(REPORT))
		{
			Hashtable<Object, Object > map9 = new Hashtable<Object, Object>();
			Hashtable<Object, Object > newmap = new Hashtable<Object, Object>();
			Query queryobj= new Query();
			queryobj.initPool();
			Vector<Object> conditionvector =new Vector<Object>();
			conditionvector.add(masterobject.getmasterHashtabel());
			queryobj.SetConditionmap(conditionvector);
			queryobj.setReportname(gid.getItem(value));
			Vector<Object>PropertyVector =new Vector<Object>();
			PropertyVector.add(masterobject.getmasterHashtabel());
			queryobj.SetConditionmap(PropertyVector);			
			queryobj.initializeData();
			queryobj.getResult();
			String buffer[]=queryobj.getbuffer();
			if(buffer.length>1)
				return buffer[1]; 
		}
		return "error";	
	}
}